# one run of a search, including most importantly the result
class SearchRun < ActiveRecord::Base
  belongs_to(:search)
  belongs_to(:timespan)
  
  def self.last_run
    find(:first, :order => "created_at desc").updated_at rescue nil
  end
  
  # updates or creates the search run with the given search id and timespan id 
  # with the given num and created_at time
  def self.update_run(search_id, timespan_id, num)
    # we can use replace into because there is a unique key on (timespan_id,search_id)
    connection.execute("replace into search_runs(timespan_id, search_id, num, created_at) values (#{timespan_id}, #{search_id}, #{num}, '#{Time.now.utc.basic_format}')")
  end
end
